#include<iostream>
#include<pthread.h>
#include<unistd.h>
#include<cstring>
#include<errno.h>
using namespace std;

int g_val=0;

// void* threadRoutine(void * args)
// {
//     int i=0;
//     while(true)
//     {
//         cout<<(char*)args<<" : "<<g_val<<"  &:  "<<&g_val<<endl;
//         g_val++;
//         sleep(1);
//         if(i++==5)break;
//     }
//     pthread_exit((void*)10);
//     cout<<"new thread quit..."<<endl;
// }

void* threadRoutine(void * args)
{
    //int i=0;
    while(true)
    {
        cout<<(char*)args<<" : "<<g_val<<"  &:  "<<&g_val<<endl;
        g_val++;
        sleep(1);
        //if(i++==5)break;
    }
    return (void*)2;
}

int main()
{
    pthread_t tid;//本质是一个地址
    pthread_create(&tid,nullptr,threadRoutine,(void*)"thread 1");
    int i=0;
    while(true)
    {
        cout<<"main thread : "<<g_val<<"  &:  "<<&g_val<<endl;
        sleep(1);
        if(i++==5) break;
    }
    pthread_cancel(tid);
    cout<<"pthread cancel:"<<tid<<endl;//线程被取消，退出码为-1。
    int* ret=nullptr;
    pthread_join(tid,(void**)&ret);//默认阻塞等待新进程退出
    cout<<"main thread wait done ... new thread quit:"<<(long long)ret<<endl;//打印退出码
    return 0;
}